package kassiopeia.agent;

import java.util.ArrayList;
import java.util.Arrays;

import kassiopeia.commons.model.task.Connection;
import kassiopeia.commons.model.task.ParameterInstance;
import kassiopeia.commons.model.task.PluginInstance;
import kassiopeia.commons.model.task.TaskDescriptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.stereotype.Component;

@Component
@ManagedResource
public class KassiopeiaAgentMBean {

    @Autowired
    private KassiopeiaAgent kassiopeiaAgent;

    @ManagedOperation
    public void stop() {
        kassiopeiaAgent.stop();
    }

    @ManagedOperation
    public void shutdown() {
        kassiopeiaAgent.shutdown();
    }

    @ManagedOperation
    public String getAgentId() {
        return kassiopeiaAgent.getAgentId();
    }

    @ManagedOperation
    public boolean isPingEnabled() {
        return kassiopeiaAgent.isPingEnabled();
    }

    //TODO - REMOVE

    @ManagedOperation
    public void startTestTask() {
        kassiopeiaAgent.start(prepareSampleTask());
    }

    
    private TaskDescriptor prepareSampleTask() {
        TaskDescriptor task = new TaskDescriptor();

        task.setName("SimpleTask");

        PluginInstance i1 = new PluginInstance("p1", "sample-plugin", "kassiopeia", "1.0");
        i1.setConfiguration(new ArrayList<ParameterInstance>());

        PluginInstance i2 = new PluginInstance("p2", "sample-plugin", "kassiopeia", "1.0");
        i2.setConfiguration(new ArrayList<ParameterInstance>());

        PluginInstance i3 = new PluginInstance("dp", "sample-dp-plugin", "kassiopeia", "1.0");
        i3.setConfiguration(new ArrayList<ParameterInstance>());

        Connection c1 = new Connection("dp", "out", "p1");
        Connection c2 = new Connection("p1", "out", "p2");
        Connection c3 = new Connection("p2", "out", "p1");

        task.setPlugins(Arrays.asList(i1, i2, i3));
        task.setConnections(Arrays.asList(c1, c2, c3));

        return task;
    }
}
